package com.stnet.dao.enterprice.system;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;

import com.modules.commons.Pager;
import com.stnet.bases.BaseDaoImpl;
import com.stnet.entity.WxSendContent;

public class WxSendContentDaoImpl extends BaseDaoImpl implements WxSendContentDao{

	@Override
	public void save(WxSendContent wsc) {
		
		getHibernateTemplate().save(wsc);
		
	}

	@Override
	public void update(WxSendContent wsc) {
		getHibernateTemplate().update(wsc);
	}

	@Override
	public void delete(Long id) {
		getHibernateTemplate().delete(get(id));
	}

	public Pager findWxSendContentByPage(int pageSize, int pageNo) {
		Session session = null;
		Pager pager = null;

		session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(WxSendContent.class);

		criteria.addOrder(Order.desc("oderDate"));
		int rowCount = ((Integer) criteria
				.setProjection(Projections.rowCount()).uniqueResult())
				.intValue();
		criteria.setProjection(null);
		int startIndex = pageSize * (pageNo - 1);
		criteria.setFirstResult(startIndex);
		criteria.setMaxResults(pageSize);
		List result = criteria.list();
		pager = new Pager(pageSize, pageNo, rowCount, result);

		session.close();

		return pager;
	}

	@Override
	public WxSendContent get(Long id) {
		return (WxSendContent) getHibernateTemplate().get(WxSendContent.class, id);
	}

}
